package com.zxe.noticeserver.controller.admin;

import com.zxe.commonserver.result.PageResult;
import com.zxe.commonserver.result.Result;
import com.zxe.noticeserver.pojo.NoticeDTO;
import com.zxe.noticeserver.pojo.NoticePageQueryDTO;
import com.zxe.noticeserver.pojo.NoticeVO;
import com.zxe.noticeserver.service.NoticeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 公告管理
 */
@RestController
@RequestMapping("/admin")
@Api(tags = "公告相关接口")
@Slf4j
public class NoticeController {

    @Autowired
    private NoticeService noticeService;

    /**
     * 新增公告
     * @param noticeDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增公告")
    public Result addNotice(@RequestBody NoticeDTO noticeDTO) {
        log.info("新增公告：{}", noticeDTO);
        noticeService.addNotice(noticeDTO);
        return Result.success();
    }

    /**
     * 公告分页查询
     * @param noticePageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("公告分页查询")
    public Result<PageResult> page(NoticePageQueryDTO noticePageQueryDTO) {
        log.info("公告分页查询：{}", noticePageQueryDTO);
        PageResult pageResult = noticeService.pageQuery(noticePageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 批量删除公告
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("批量删除公告")
    public Result delete(@RequestParam List<Long> ids) {
        log.info("批量删除公告：{}", ids);
        noticeService.delete(ids);
        return Result.success();
    }

    /**
     * 修改公告
     * @param noticeDTO
     * @return
     */
    @PutMapping
    @ApiOperation("修改公告")
    public Result update(@RequestBody NoticeDTO noticeDTO) {
        log.info("修改公告：{}", noticeDTO);
        noticeService.update(noticeDTO);
        return Result.success();
    }

    /**
     * 设置公告可见范围
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("设置公告可见范围")
    public Result startOrStop(@PathVariable Integer status, Long id) {
        log.info("设置公告可见范围：{}, {}", status, id);
        noticeService.openOrHide(status, id);
        return Result.success();
    }

    /**
     * 根据id查询公告
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询公告")
    public Result<NoticeVO> findById(@PathVariable Long id) {
        log.info("根据id查询公告：{}", id);
        NoticeVO noticeVO = noticeService.findById(id);
        return Result.success(noticeVO);
    }
}

